/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.pdx.cs.guavadata;

import edu.pdx.cs.guavadata.subclass.GSeedFrame;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 *
 * @author VuTran
 */
public class GSeed extends ArrayList<GSeedFrame> {

    public String root;
    int level;
    public String source;
    public String catalog;

    public GSeed(String root, String source, String catalog) {
        super();
        this.level = 0;
        this.root = root;
        this.source = source;
        this.catalog = catalog;
    }


    public void push(GSeedFrame gSeedFrame) {
        super.add(gSeedFrame);
        level++;
    }

    public GSeedFrame peek() {
        if (super.size() > 0)
            return super.get(super.size() - 1);
        else
            return null;
    }

    public GSeedFrame peekAdvance() {
        if (needToAdvance())
            return super.get(level++);
        else
            return null;
    }

    public void pop() {
        if (super.size() > 0)
            super.remove(super.size() - 1);
    }

    public UUID nextId() {
        if (this.level < this.size())
            return super.get(level).g;
        return UUID.nameUUIDFromBytes("000".getBytes());
    }

    public boolean needToAdvance() {
        return this.level < this.size();
    }

    public String nextForm() {
        if (this.level < this.size())
            return super.get(level).form;
        return null;
    }

    public List<String> toList() {
        List<String> ls = new ArrayList<String>();
        for (GSeedFrame gsf : (GSeedFrame[]) super.toArray()) {
            ls.addAll(gsf.toList());
        }
        return ls;
    }

    public static GSeed fromString(String s) {
        String[] ls = s.split("~");

        // Pull the first entry's form name as the root
        GSeed gs = new GSeed(ls[0], ls[1], ls[2]);

        // For all of the other entries, each corresponds to a frame, so build
        for (int i = 1; i < ls.length; i++)
            gs.add(GSeedFrame.fromString(ls[i]));

        // Return the result, noting that the level is still set to 0 because we used Add instead of Push
        return gs;
    }


}
